#!/bin/bash
################### 检测nginx是否存活脚本 #####################
# 执行之前：
# 	1. 去除特殊字符：sed -i 's/\r//' check_nginx.sh
# 	2. 添加执行权限：chmod +x check_nginx.sh
# 执行命令：./check_nginx.sh
# 执行之后：
# 脚本说明：需要修改nginx实际安装路径
########################################################

nginx_counter=$(ps -ef|grep nginx | grep master | wc -l)
echo "nginx_counter $nginx_counter"
if [ "${nginx_counter}" -lt 1 ]; then
  echo "nginx is not alive, try starting nginx"
  cd /usr/local/nginx/sbin
  ./nginx
  sleep 3
  nginx_counter=$(ps -ef|grep nginx | grep master | wc -l)
  if [ "${nginx_counter}" -lt 1 ]; then
    echo "nginx is still not alive, stop local keepalived"
    systemctl stop keepalived
  fi
else 
  echo "nginx is still alive, check status is OK!"
fi
